package programita;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author educacionit
 */
/**
 * @author Leo
 *
 */
public class AdminDeDatos {

    private static AdminDeDatos instance = null;
    private boolean ocupado;
    private Connection conn = null;

    private AdminDeDatos() {
        try {
            conn = AdminDeDatos.createConnection();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
        }
    }

    public static AdminDeDatos getInstance() {
        if (instance == null) {
            instance = new AdminDeDatos();
            System.out.println("Conexion exitosa");
        }
        return instance;
    }

    public synchronized Connection getConnection() throws Exception {
        if (ocupado) {
            throw new Exception();
        }
        ocupado = true;
        return conn;

    }

 

    public void releaseConnection(Connection c) throws Exception {
        if (conn == c) {
            ocupado = false;
        } else {
            throw new Exception();
        }
    }


    public boolean validarUsuario(String nombreUs,String contraseniaUs) {
        Connection c = null;
        boolean valido = false;
        try {
            c = this.getConnection();
            String laConsulta = "SELECT * FROM usuarios";
            Statement stmtConsulta = c.createStatement();
            ResultSet rs = stmtConsulta.executeQuery(laConsulta);

            while ((rs.next())& (!valido)) {
                //System.out.println("Nombre: " + rs.getString("nombre") + " -- " + "Apellido: " + rs.getString("contrasenia"));
                if ((rs.getString("Usuario").compareTo(nombreUs) == 0) && (rs.getString("Clave").compareTo(contraseniaUs) == 0)) {
                    valido = true;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                this.releaseConnection(c);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            return valido;
        }
    }




    public static Connection createConnection() throws Exception {
        //java.util.Properties a = new java.util.Properties;

        // Establece el nombre del driver a utilizar
       // String dbDriver = �sun.jdbc.odbc.JdbcOdbcDriver�;
        //String dbDriver = "com.mysql.jdbc.Driver";

        // Establece la conexion a utilizar contra la base de datos
        String dbConnString = "jdbc:mysql://Administrador/Alumnos";
        //String dbConnString = "jdbc:mysql://mysql5.000webhost.com/a8874963_blakjak";

        // Establece el usuario de la base de datos
        String dbUser = "root";

        // Establece la contraseña de la base de datos
        String dbPassword = "H0#ac1O";

        // Establece el driver de conexion
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
       // Class.forName(dbDriver).newInstance();
        
        // Retorna la conexion
       
        return DriverManager.getConnection("jdbc:odbc:Alumnos","Administrador", dbPassword);
       //return DriverManager.getConnection(dbConnString,"", dbPassword);
        

    }
    
    /*
    public void actualizarBD(JugadorHumano player){
        Connection c = null;
        try{
		c = this.getConnection();

		Statement stmt = c.createStatement();
		stmt.execute("UPDATE usuarios SET saldo = "+Float.toString(player.getSaldo())+
                             " WHERE nombre = '"+player.getNombre()+"'");
	}catch(Exception e){
		e.printStackTrace();
	}finally{
            try {
                // System.out.println(AdminConecciones.getInstance());
                this.releaseConnection(c);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
	}
    }*/
    /*  public float getSaldo(JugadorHumano player1) {
    Connection c = null;
    boolean encontrado = false;
    float saldo = 0;
    try {
        c = this.getConnection();
        String laConsulta = "SELECT * FROM usuarios";
        Statement stmtConsulta = c.createStatement();
        ResultSet rs = stmtConsulta.executeQuery(laConsulta);

        while ((rs.next()) && (!encontrado)) {
            //System.out.println("Nombre: " + rs.getString("nombre") + " -- " + "Apellido: " + rs.getString("contrasenia"));
            if (rs.getString("nombre").compareTo(player1.getNombre()) == 0) {
                saldo = rs.getFloat("saldo");
                encontrado = true;
                
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            this.releaseConnection(c);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return saldo;
    }
}*/

//public static void main(String[]args){
//    try {
//        System.out.println(AdminDeDatos.createConnection());
//    } catch (Exception ex) {
//        Logger.getLogger(AdminDeDatos.class.getName()).log(Level.SEVERE, null, ex);
//    }
//}
    
    /* public void insertarUsuario(JugadorHumano player1){
    Connection laConexion = null;
    // Define la conexion
    try {
        laConexion = this.getConnection();//Devuelve la conexion y la pone en ocupado

        // Arma la sentencia de inserción y la ejecuta
        String laInsercion = "INSERT INTO usuarios (nombre,contrasenia,saldo) VALUES ('" + player1.getNombre() + "', '" + player1.getContrasenia() + "',"+Float.toString(player1.getSaldo())+")";
        Statement stmtInsercion = laConexion.createStatement();
        stmtInsercion.execute(laInsercion);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            // Cierra el Statement y la Connection
            //stmtInsercion.close();
            this.releaseConnection(laConexion);
        } catch (Exception ex) {
            Logger.getLogger(AdminDeDatos.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}*/
    
    /*  public boolean validarRegistro(String nombreUs,String contraseniaUs) {
    Connection c = null;
    boolean valido = true;
    try {
        c = this.getConnection();
        String laConsulta = "SELECT * FROM Usuarios";
        Statement stmtConsulta = c.createStatement();
        ResultSet rs = stmtConsulta.executeQuery(laConsulta);

        while ((rs.next()) && (valido)) {
            //System.out.println("Nombre: " + rs.getString("nombre") + " -- " + "Apellido: " + rs.getString("contrasenia"));
            if ((rs.getString("nombre").compareTo(nombreUs) == 0)) {
                valido = false;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            this.releaseConnection(c);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return valido;
    }
}*/
}

